package com.jiukuaidao.zhulin.module.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.adapter.SwipeAdapter;
import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiHistoryAddressInfo.AddressInfo;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.jiukuaidao.zhulin.module.address.contract.AddressManagerContract;
import com.jiukuaidao.zhulin.module.address.contract.DeleteAddressContract;
import com.jiukuaidao.zhulin.module.address.presenter.AddressMangerPresenter;
import com.jiukuaidao.zhulin.module.address.presenter.DeleteAddressPresenter;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/30
 * Description : 收货地址管理
 */
public class AddressManagerActivity extends BaseActivity<AddressManagerContract.Presenter> implements View.OnClickListener, AddressManagerContract.View, DeleteAddressContract.View {
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.address_recycle)
    XRecyclerView mRecycleView;
    @BindView(R.id.change_rl_add_address)
    RelativeLayout mAddAddress;

    private BaseXrecyclerViewAdapter<AddressInfo> mBaseAdapter;
    private String mDelAddressId;
    private DeleteAddressPresenter mDelPresenter;
    private String mFromActivity;
    private int mIndex = 1;
    private int mRefreshOrLoad = AddressManagerContract.REFRESH;
    public static AddressManagerActivity mInstance;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_address_manager;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mFromActivity = bundle.getString("fromActivity");
    }

    @Override
    protected void init() {
        mInstance = this;
        mTitileText.setText(R.string.manager_address);
        setOnClickListener();
        new AddressMangerPresenter(TaskProvider.provideAddressRepository(this), this);
        mPresenter.start();
        mDelPresenter = new DeleteAddressPresenter(TaskProvider.provideTasksRepository(this), this);
    }

    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.change_rl_add_address:
                if (mFromActivity.equals("shopCart")) {   // 从购物车过来添加商品
                    bundle.putString("fromModel", "shopCart");
                } else {
                    bundle.putString("fromModel", "addAddress");
                }
                intentJumpForResult(AddAddressActivity.class, 0, bundle);
                break;
            case R.id.titile_left_imageview:
                finish();
                break;
        }

    }

    public void setOnClickListener() {
        mAddAddress.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
    }

    @Override
    public void getAddressInfo(List<AddressInfo> shoppingAddressList) {
        if (null != mBaseAdapter) {
            mBaseAdapter.setData(shoppingAddressList);
            mBaseAdapter.notifyDataSetChanged();
        } else {
            initAddress(shoppingAddressList);
        }

    }

    public void initAddress(final List<AddressInfo> shoppingAddressList) {
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mRecycleView.setPullRefreshEnabled(false);
        mRecycleView.setLoadingMoreEnabled(false);
        mBaseAdapter = new SwipeAdapter<AddressInfo>(this, R.layout.item_edit_address, shoppingAddressList, mRecycleView) {

            @Override
            public void updateUI(BaseHolder holder, int position, AddressInfo data) {
                TextView address = holder.getView(R.id.tv_address_info);
                TextView mobile = holder.getView(R.id.tv_phone_info);
                TextView consignee = holder.getView(R.id.tv_name_info);
                address.setText(data.address);
                mobile.setText(data.mobile);
                consignee.setText(data.consignee);

            }

            @Override
            public int getDeleteViewId() {
                return R.id.del_address;
            }

            @Override
            protected void setListener(final BaseHolder holder, final int position) {
                super.setListener(holder, position);
                ImageView ivEdit = holder.getView(R.id.iv_modify);
                ivEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("fromModel", "editAddress");
                        bundle.putSerializable("AddressInfo", shoppingAddressList.get(position));
                        intentJumpForResult(AddAddressActivity.class, 0, bundle);
                    }
                });
                holder.getView(R.id.address_layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 购物车切换地址
                        if (mFromActivity.equals("shopCart")) {
                            AddressInfo addressInfo = shoppingAddressList.get(position);
                            //当前位置存入首选项
                            AddressInfoUtil.cleanAddressInfo();
                            PreAddressBean mAddressInfo = new PreAddressBean();
                            mAddressInfo.longitude = addressInfo.longitude;
                            mAddressInfo.latitude = addressInfo.latitude;
                            mAddressInfo.city = addressInfo.city;
                            mAddressInfo.cityCode = addressInfo.baiduCityCode;
                            mAddressInfo.address = addressInfo.address;
                            AddressInfoUtil.saveAddressInfo(mAddressInfo);
                            EventBus.getDefault().post(addressInfo);
                            AddressManagerActivity.this.finish();
                        }
                    }
                });

            }

            @Override
            public void deleteSuc(AddressInfo addressInfo) {
                mDelAddressId = addressInfo.addressId;
                mDelPresenter.loadData();
            }
        };
        mRecycleView.setAdapter(mBaseAdapter);
        mRecycleView.setOnRefreshListener(new XRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshOrLoad = AddressManagerContract.REFRESH;
                mIndex = 1;
                mPresenter.start();
                mRecycleView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mRefreshOrLoad = AddressManagerContract.LOADMORE;
                mIndex++;
                mPresenter.start();
                mRecycleView.loadMoreComplete();
            }
        });
    }

    @Override
    public String getAddressId() {
        return mDelAddressId;
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public int index() {
        return mIndex;
    }

    @Override
    public int refreshOrLoad() {
        return mRefreshOrLoad;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.start();
    }
}
